#include "matrix.h"
#include "ui_matrix.h"
#include "algorithm"
#include "math.h"
#include"stdio.h"
#include"stdlib.h"
matrix::matrix(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::matrix)
{ ui->setupUi(this);
}

matrix::~matrix()
{
    delete ui;
}
double n,det=1;
double A[5][5]={0},B[5][5]={0},C[5][5]={0},a[5][5]={0},b[5][5]={0},c[5][5]={0};
void matrix::on_dim_editingFinished()
{QString m=ui->dim->text();
  n=m.toDouble();
 }
void matrix::on_A00_editingFinished()
{  QString a=ui->A00->text();
    A[0][0]=a.toDouble();
}
void matrix::on_A01_editingFinished()
{  QString a=ui->A01->text();
    A[0][1]=a.toDouble();
}
void matrix::on_A02_editingFinished()
{  QString a=ui->A02->text();
    A[0][2]=a.toDouble();
}
void matrix::on_A03_editingFinished()
{  QString a=ui->A03->text();
    A[0][3]=a.toDouble();
}
void matrix::on_A04_editingFinished()
{  QString a=ui->A04->text();
    A[0][4]=a.toDouble();
}
void matrix::on_A10_editingFinished()
{  QString a=ui->A10->text();
    A[1][0]=a.toDouble();
}
void matrix::on_A11_editingFinished()
{  QString a=ui->A11->text();
    A[1][1]=a.toDouble();
}
void matrix::on_A12_editingFinished()
{  QString a=ui->A12->text();
    A[1][2]=a.toDouble();
}
void matrix::on_A13_editingFinished()
{  QString a=ui->A13->text();
    A[1][3]=a.toDouble();
}
void matrix::on_A14_editingFinished()
{  QString a=ui->A14->text();
    A[1][4]=a.toDouble();
}
void matrix::on_A20_editingFinished()
{  QString a=ui->A20->text();
    A[2][0]=a.toDouble();
}
void matrix::on_A21_editingFinished()
{  QString a=ui->A21->text();
    A[2][1]=a.toDouble();
}
void matrix::on_A22_editingFinished()
{  QString a=ui->A22->text();
    A[2][2]=a.toDouble();
}
void matrix::on_A23_editingFinished()
{  QString a=ui->A23->text();
    A[2][3]=a.toDouble();
}
void matrix::on_A24_editingFinished()
{  QString a=ui->A24->text();
    A[2][4]=a.toDouble();
}
void matrix::on_A30_editingFinished()
{  QString a=ui->A30->text();
    A[3][0]=a.toDouble();
}
void matrix::on_A31_editingFinished()
{  QString a=ui->A31->text();
    A[3][1]=a.toDouble();
}
void matrix::on_A32_editingFinished()
{  QString a=ui->A32->text();
    A[3][2]=a.toDouble();
}
void matrix::on_A33_editingFinished()
{  QString a=ui->A33->text();
    A[3][3]=a.toDouble();
}
void matrix::on_A34_editingFinished()
{  QString a=ui->A34->text();
    A[3][4]=a.toDouble();
}
void matrix::on_A40_editingFinished()
{  QString a=ui->A40->text();
    A[4][0]=a.toDouble();
}
void matrix::on_A41_editingFinished()
{  QString a=ui->A41->text();
    A[4][1]=a.toDouble();
}
void matrix::on_A42_editingFinished()
{  QString a=ui->A42->text();
    A[4][2]=a.toDouble();
}
void matrix::on_A43_editingFinished()
{  QString a=ui->A43->text();
    A[4][3]=a.toDouble();
}
void matrix::on_A44_editingFinished()
{  QString a=ui->A44->text();
    A[4][4]=a.toDouble();
}
void matrix::on_B00_editingFinished()
{  QString b=ui->B00->text();
    B[0][0]=b.toDouble();
}
void matrix::on_B01_editingFinished()
{  QString b=ui->B01->text();
    B[0][1]=b.toDouble();
}
void matrix::on_B02_editingFinished()
{  QString b=ui->B02->text();
    B[0][2]=b.toDouble();
}
void matrix::on_B03_editingFinished()
{  QString b=ui->B03->text();
   B[0][3]=b.toDouble();
}
void matrix::on_B04_editingFinished()
{  QString b=ui->B04->text();
    B[0][4]=b.toDouble();
}
void matrix::on_B10_editingFinished()
{  QString b=ui->B10->text();
   B[1][0]=b.toDouble();
}
void matrix::on_B11_editingFinished()
{  QString b=ui->B11->text();
    B[1][1]=b.toDouble();
}
void matrix::on_B12_editingFinished()
{  QString b=ui->B12->text();
    B[1][2]=b.toDouble();
}
void matrix::on_B13_editingFinished()
{  QString b=ui->B13->text();
    B[1][3]=b.toDouble();
}
void matrix::on_B14_editingFinished()
{  QString b=ui->B14->text();
   B[1][4]=b.toDouble();
}
void matrix::on_B20_editingFinished()
{  QString b=ui->B20->text();
    B[2][0]=b.toDouble();
}
void matrix::on_B21_editingFinished()
{  QString b=ui->B21->text();
    B[2][1]=b.toDouble();
}
void matrix::on_B22_editingFinished()
{  QString b=ui->B22->text();
   B[2][2]=b.toDouble();
}
void matrix::on_B23_editingFinished()
{  QString b=ui->B23->text();
    B[2][3]=b.toDouble();
}
void matrix::on_B24_editingFinished()
{  QString b=ui->B24->text();
   B[2][4]=b.toDouble();
}
void matrix::on_B30_editingFinished()
{  QString b=ui->B30->text();
    B[3][0]=b.toDouble();
}
void matrix::on_B31_editingFinished()
{  QString b=ui->B31->text();
    B[3][1]=b.toDouble();
}
void matrix::on_B32_editingFinished()
{  QString b=ui->A32->text();
    B[3][2]=b.toDouble();
}
void matrix::on_B33_editingFinished()
{  QString b=ui->B33->text();
    B[3][3]=b.toDouble();
}
void matrix::on_B34_editingFinished()
{  QString b=ui->B34->text();
    B[3][4]=b.toDouble();
}
void matrix::on_B40_editingFinished()
{  QString b=ui->B40->text();
    B[4][0]=b.toDouble();
}
void matrix::on_B41_editingFinished()
{  QString b=ui->B41->text();
   B[4][1]=b.toDouble();
}
void matrix::on_B42_editingFinished()
{  QString b=ui->B42->text();
    B[4][2]=b.toDouble();
}
void matrix::on_B43_editingFinished()
{  QString b=ui->B43->text();
    B[4][3]=b.toDouble();
}
void matrix::on_B44_editingFinished()
{  QString b=ui->B44->text();
    B[4][4]=b.toDouble();
}
double matrix::t(int n,int**A)
{  double ta=0.000000;
    for(int i=0;i<n;i++)
    {   ta+=A[i][i];
     }
    return ta;
    }

void matrix::step(int n,double&sign,double**&A)
{for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)//从对角线上的元素开始
        {
            int sum = 0, y = 0;//sum用来控制这一列是否都是零，y用来控制消元结束后直接跳出j的这层循环，进入到下一行
            if (A[i][j] == 0)//如果对角线元素为零
            {
                for (int k = i + 1; k < n; k++)//遍历其下方元素
                {
                    if (A[k][j] != 0)//如果下方有不为零的元素
                    {
                        double tmp;
                        for (int p = j; p < n; p++)
                            tmp = A[k][p], A[k][p] = A[i][p], A[i][p] = tmp, sign++;//两行交换
                        break;//并直接跳出循环，进行消元
                    }
                }
                //没跳出循环说明下方的元素一直为零，说明这一列已经全为零，则跳到下一列
                continue;//直接返回循环开始
            }
            double x;
            for (int k = i + 1; k < n; k++)
            {
                x = A[k][j] / A[i][j];
                for (int p = j; p < n; p++)//进行消元
                {
                    A[k][p] -= x * A[i][p];
                }
                y = 1;
            }
            if (y == 1)//y=1说明已经完成了消元，跳出j的循环
                break;
        }
    }

}
double matrix::det(int n,double**a,double det)
{double sign = 0;
    matrix::step(n, sign,**a);
        for (int i = 0; i < n; i++)
        {
            det *= a[i][i];
        }
return det*pow(-1,sign);
}
double matrix::rank(int n,double**a)
{
    int rank = 0;
    double sign;
    matrix::step(n,sign,a);
    for (int i = 0; i < n; i++)
        {
            int sum = 0;
            for (int j = 0; j < n; j++)
                sum += (a[i][j] != 0);
            sum == 0 ? rank++ : rank = rank;
        }
        rank = n - rank;
        return rank;
 }
double** matrix::inverse(int n,double**a)
{  double **b;
    b=(int**)malloc(n*sizeof(int*));
    for(int i=0;i<n;i++)
    {b[i]=(int*)malloc(2*sizeof(int));}
    for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                i == j ? b[i][j] = 1 : b[i][j] = 0;
        }
        for (int i = 0; i < n; i++)
        {
            if (a[i][i] != 0)//如果对角线上的元素不为零
            {
                for (int j = i + 1; j < n; j++)//从对角线下的元素开始
                {
                    double x;
                    x = a[j][i] / a[i][i];//依次计算该元素与对角线元素的倍数
                    for (int k = i + 1; k < n; k++)
                        a[j][k] -= x * a[i][k];//将第i行其余元素的x倍减到第j行
                    for (int k = 0; k < n; k++)
                        b[j][k] -= x * b[i][k];
                    a[j][i] = 0;//将第j行对角线下的元素赋值为0
                }
            }
            else//如果对角线元素为零
            {
                int sum = 0;//用来控制是否对角线以下已经全为零
                for (int p = i + 1; p < n; p++)//寻找该列不为零的元素
                {
                    if (a[p][i] != 0)
                    {
                        double tmp;
                        for (int q = i; q < n; q++)
                        {
                            tmp = a[i][q], a[i][q] = a[p][q], a[p][q] = tmp;//将两行进行互换
                            sum = 1;
                        }
                        for (int q = 0; q < n; q++)
                            tmp = b[i][q], b[i][q] = b[p][q], b[p][q] = tmp;
                        sign++;//换行要变号！！！
                        break;
                    }
                    else
                        sum = 0;
                }
                if (sum == 0)
                    break;//sum=0意味着对角线之下全是0，此时行列式已经为零，不必进行下面的消元
                for (int j = i + 1; j < n; j++)
                {
                    double x;
                    x = a[j][i] / a[i][i];
                    for (int k = i + 1; k < n; k++)
                        a[j][k] -= x * a[i][k];
                    a[j][i] = 0;
                }
            }
            for (int i = n - 1; i >= 0; i--)
                    {
                        for (int j = i + 1; j < n; j++)
                            a[i][j] = (1.0 * a[i][j]) / (1.0 * a[i][i]);  //对角线化为1
                        for (int j = 0; j < n; j++)
                            b[i][j] = (1.0 * b[i][j]) / (1.0 * a[i][i]);
                        a[i][i] = 1;
                    }

            for (int i = n - 1; i >= 0; i--)
                    {
                        double x;
                        for (int j = i - 1; j >= 0; j--)//将左半部分变为初等矩阵
                        {
                            x = a[j][i] / a[i][i];
                            a[j][i] -= x * a[i][i];
                            for (int k = 0; k < n; k++)
                                b[j][k] -= x * b[i][k];
                            a[j][i] = 0;
                        }}
    return b;
}}
double** matrix::addition(int n,double**a,double**b)
{double**c;
    c=(int**)malloc(n*sizeof(int*));
    for(int i=0;i<n;i++)
    {c[i]=(int*)malloc(2*sizeof(int));}
    for(int i=0;i<n;i++ )
    { for(int j=0;j<n;j++)
        {  c[i][j]=a[i][j]+b[i][j];
        }
       }
    return c;
}
double** matrix::subtraction(int n,double**a,double**b)
{double**c;
    c=(int**)malloc(n*sizeof(int*));
    for(int i=0;i<n;i++)
    {c[i]=(int*)malloc(2*sizeof(int));}
    for(int i=0;i<n;i++ )
    { for(int j=0;j<n;j++)
        {  c[i][j]=a[i][j]-b[i][j];
        }
       }
    return c;
}
double** matrix::multiplication(int n,double**a,double**b)
{double**c;
        c=(int**)malloc(n*sizeof(int*));
        for(int i=0;i<n;i++)
        {c[i]=(int*)malloc(2*sizeof(int));}
        for (int i = 0; i < n;i++)
            {
                for (int j = 0; j <n;j++)
                {
                    c[i][j] = 0;
                    for (int k = 0; k <n;k++)
                        c[i][j] += a[i][k] * b[k][j];
                }
            }

    return c;
}
double**matrix::division(int n,double**a,double**b)
{{double**c;
        c=(int**)malloc(n*sizeof(int*));
        for(int i=0;i<n;i++)
        {c[i]=(int*)malloc(2*sizeof(int));}
       double**d=matrix::inverse(n,b);
      c=matrix::multiplication(n,a,d);
      return c;
}
}
